//
//  PaperAnalyModel.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/10.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAnalyModel.h"

@implementation PaperAnalyModel

+ (NSDictionary *)objectClassInArray {
    return @{@"ques_stem"  : [PaperTypeModel class],
             @"option"     : [PaperOptionModel class],
             @"right_answer_wenda" : [PaperTypeModel class],
             @"analysis"   : [PaperTypeModel class],
             @"abstract"   : [PaperTypeModel class],
             @"knows_name" : [NSString class]};
}

- (void)mj_keyValuesDidFinishConvertingToObject {
    [super mj_keyValuesDidFinishConvertingToObject];
    
    [self.optionLayouts enumerateObjectsUsingBlock:^(PaperAnswerOptionModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        PaperOptionModel *optionModel = self.option[idx];
        
        NSString *userAnswer ;
        if (self.user_answer) {
            userAnswer = self.user_answer;
        }else{
            userAnswer = @"";
        }
        if (self.ques_model == 0) {//单选
            if (self.user_answer_right == 1) {//单选答对
                
                if ([self.right_answer isEqualToString:optionModel.answer]) {
                    model.type = PaperOptionTypeRight;
                }else{
                    model.type = PaperOptionTypeNormal;
                }
                
            }else{//单选答错
                if (userAnswer.length > 0 && [userAnswer isEqualToString:optionModel.answer]) {
                        model.type = PaperOptionTypeSelect;
                }else if(userAnswer.length >0 && [self.right_answer isEqualToString:optionModel.answer]){
                    model.type = PaperOptionTypeRight;
                    
                }else if(userAnswer.length >0 && ![self.right_answer isEqualToString:optionModel.answer]){
                    model.type = PaperOptionTypeWrong;
                    
                }else if (userAnswer.length == 0){
                    model.type = PaperOptionTypeNormal;
                    
                }
            }
//            if (userAnswer.length > 0 && [self.right_answer isEqualToString:optionModel.answer]) {
//                model.type = PaperOptionTypeSelect;
//            }else if(userAnswer.length >0 && ![self.right_answer isEqualToString:optionModel.answer]){
//                model.type = PaperOptionTypeWrong;
//
//            }else if (userAnswer.length == 0){
//                model.type = PaperOptionTypeNormal;
//
//            }
//            else if (userAnswer.length == 0 && ![self.right_answer containsString:optionModel.answer]){
//                model.type = PaperOptionTypeNormal;
//
//            }
        }else if(self.ques_model == 1){//多选
            
//            NSArray *userArray = [userAnswer componentsSeparatedByString:@","];
            
//            for (NSString *answer in userArray) {

//            }
            if ([self.right_answer containsString:optionModel.answer]) {
                model.type = PaperOptionTypeRight;
            }else if(![self.right_answer containsString:optionModel.answer]){
                if ([userAnswer containsString:optionModel.answer]) {
                    model.type = PaperOptionTypeWrong;
                }else{
                    model.type = PaperOptionTypeNormal;
                }
                
            }
            
//            if (userAnswer.length > 0 && [self.right_answer containsString:optionModel.answer]) {
//                model.type = PaperOptionTypeSelect;
//            }else if(userAnswer.length >0 && ![self.right_answer containsString:optionModel.answer]){
//                model.type = PaperOptionTypeWrong;
//
//            }else if (userAnswer.length == 0 && [self.right_answer containsString:optionModel.answer]){
//                model.type = PaperOptionTypeRight;
//
//            }else if (userAnswer.length == 0 && ![self.right_answer containsString:optionModel.answer]){
//                model.type = PaperOptionTypeNormal;
//
//            }
            
        }
 

    }];
    
    //问答题答案
    if (self.right_answer_wenda && self.right_answer_wenda.count>0) {
        [self.right_answer_wenda insertObject:[PaperTypeModel createWendaAnswer] atIndex:0];
        self.wendaLayout = [YYTextLayout layoutWithTexts:self.right_answer_wenda];
    }

    // 每道题前面增加【单选题】or【多选题】
    [self.analysis insertObject:[PaperTypeModel createQuestAnaly] atIndex:0];
    self.analyLayout = [YYTextLayout layoutWithTexts:self.analysis];
    
    
    
    
    NSString *str = @"D，本试题被123位同学做过！";
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString: str]; //创建一个NSMutableAttributedString
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)]; // 关键步骤，设置指定位置文字的颜色
    
    self.attributedText = attributedStr;
}

@end
